/**
 * Vars for checking numeric/alpha etc...
 */
var numb = '0123456789';
var lc   = 'abcdefghijklmnopqrstuvwxyz';
var uc   = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var nc   = lc + uc + "-'"; // acceptable characters in first/middle names
var lnc  = lc + uc + "-'. "; // acceptable characters in last names

/**
 * colors for warnings etc...
 */
var color_ok   = '#0FFFA3';
var color_fail = '#FFA0B0';

/**
 * Highlight specified object
 * @param object
 * @param string message
 * @return void
 */
function highlightObject(obj,msg) {
	obj.style.background = color_fail;
	alert(msg);
	obj.focus();
	}

/**
 * Is the needle in the haystack?
 * @param mixed needle
 * @param array haystack
 * @return bool
 */
function inArray(needle,haystack) {
	if (typeof(needle) == 'undefined') { return false; }
	if (!isArray(haystack)) { return false; }
	var key;
	for(key in haystack) {
		if (haystack[key] === needle) { return true; }
		}
	return false;
	}

/**
 * Is the specified object an array?
 * @param mixed testAr
 * @return bool
 */
function isArray(testAr) {
	return testAr && !(testAr.propertyIsEnumerable('length')) &&
	typeof testAr === 'object' && typeof testAr.length === 'number';
	}

/**
 * Is specified email address valid?
 * @param string email
 * @return bool
 */
function isEmail(email) {
	if (typeof(email) == 'undefined') { return false; }
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
	}

/**
 * Wrappers to test for numeric, alpha, alphanumeric, lowercase, etc...
 */
function isAlpha(parm)        { return isValid(parm,lc+uc);      }
function isAlphaNumeric(parm) { return isValid(parm,lc+uc+numb); }
function isLastName(parm)     { return isValid(parm,lnc);        }
function isLower(parm)        { return isValid(parm,lc);         }
function isName(parm)         { return isValid(parm,nc);         }
function isNumeric(parm)      { return isValid(parm,numb);       }
function isUpper(parm)        { return isValid(parm,uc);         }
function isUsername(parm)     { return isAlphaNumeric(parm);     }

/**
 * Sponsor names can have spaces in them...
 */
function isSponsorName(parm) {
	return isValid(parm,nc + ' ');
	}

/**
 * Used for alphanumeric testing functions.
 * @return bool
 */
function isValid(parm,val) {
	if (typeof(parm) == 'undefined') { return false; }
	if (parm == "") return false;
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1) { return false };
		}
	return true;
	}

/**
 * Rudimentary check on specified birth month/day (does not check leap years)
 * @param int month
 * @param int day
 * @return bool
 */
function isValidBirthMonthDay(month,day) {
	if (!isNumeric(month))      { return false; }
	if (!isNumeric(day))        { return false; }
	if (month < 1 || day < 1)   { return false; }
	if (month > 12)             { return false; }
	if (day > 31)               { return false; }
	switch(month) {
		case '2':
			if (day > 29) { return false; }
		break;
		case '4':
		case '6':
		case '9':
		case '11':
			if (day > 30) { return false; }
		break;
		}
	return true;
	}

/**
 * Validate zip code
 */
function isZip(parm) {
	if (!isValid(parm,numb+'-')) { return false; }
	return (parm.length == 5 || parm.length == 10);
	}

/**
 * Stop the enter key from submitting the form.
 * To use this, add the following to the document:
 * document.onkeypress = stopRKey;
 */
function stopRKey(evt) {
	var evt = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) {
		return false;
		}
	}

/**
 * Toggle an id element between display:none and display:block
 */
function toggleContent(elem) {
	var newDisplay;
	var obj = document.getElementById(elem);
	if (obj) {
		newDisplay = (obj.style.display == 'block') ? 'none' : 'block';
		obj.style.display = newDisplay;
		return true;
		}
	return false;
	}

/**
 * Camel case a word
 * @param obj
 * @return obj with value having 1st letter capitalized
 */
function ucword(obj) {
	if (typeof(obj.value) == 'string') {
		obj.value = obj.value.substr(0,1).toUpperCase() + obj.value.substr(1);
		}
	}
